﻿using Max.PH5_2008A.ShoppingCode.Domain;
using Max.PH5_2008A.ShoppingCode.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH5_2008A.ShoppingCode.API.Controllers.Member
{
    [Route("api/[controller]")]
    [ApiController]
    public class MemberAddressController : BaseController<MemberAddressEntity>
    {
        IMemberAddressService _service;
        public MemberAddressController(IMemberAddressService service) : base(service)
        {
            _service = service;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPost]
        public override IActionResult Insert(MemberAddressEntity t)
        {
            //1、判断要添加的这个地址，是否为默认地址
            if (t.Statue.Equals("1"))
            {
                //2、如果是：则需要将其它的默认地址设置为非默认
                var d = _service.Query().Where(x => x.Statue.Equals("1") && x.MemberId == t.MemberId).FirstOrDefault();
                if (d != null)
                {
                    d.Statue = "0";
                    _service.Update(d);
                }
            }

            //3、直接插入这条数据
            bool result = _service.Insert(t);

            return Ok(result);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("address")]
        public   IActionResult Query(string memberId)
        {
            var list = _service.Query().Where(x=>memberId.Equals(x.MemberId.ToString()));

            return Ok(list);
        }

    }
}
